<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { getGuessAPI } from '@/services/home'
import type { GuessListItem } from '@/types/home'
import type { PagesInfo } from '@/types/global'

// 公共组件 数据及组件独立出页面 将接口放在组件中调用
const guessList = ref<GuessListItem[]>([])
// 设置分页接口参数
const params: Required<PagesInfo> = {
  page: 31,
  pageSize: 10,
}
const finishData = ref(false)
const getGuessData = async () => {
  if (finishData.value) {
    return uni.showToast({ icon: 'none', title: '没有更多数据了~' })
  }
  const res = await getGuessAPI(params)
  // guessList.value = res.result.items // 列表数组赋值
  guessList.value.push(...res.result.items)
  if (params.page < res.result.pages) {
    params.page++
  } else {
    finishData.value = true
  }
}
// 重置数据 页码， 结束标志  list列表
const resetData = () => {
  params.page = 1
  guessList.value = []
  finishData.value = false
}
defineExpose({
  getGuessData,
  resetData,
})
onMounted(() => getGuessData())
</script>

<template>
  <!-- 猜你喜欢 -->
  <div class="caption">
    <span class="text">猜你喜欢</span>
  </div>
  <div class="guess">
    <navigator
      class="guess-item"
      v-for="item in guessList"
      :key="item.id"
      :url="`/pages/goods/goods?id=4007498`"
    >
      <img class="image" mode="aspectFill" :src="item.picture" />
      <div class="name">{{ item.name }}</div>
      <div class="price">
        <span class="small">¥</span>
        <span>{{ item.price }}</span>
      </div>
    </navigator>
  </div>
  <div class="loading-text">{{ finishData ? '没有更多数据了~' : '正在加载...' }}</div>
</template>

<style lang="scss">
:host {
  display: block;
}
/* 分类标题 */
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  padding: 36rpx 0 40rpx;
  font-size: 32rpx;
  color: #262626;
  .text {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;

    &::before,
    &::after {
      content: '';
      width: 20rpx;
      height: 20rpx;
      background-image: url(@/static/images/bubble.png);
      background-size: contain;
      margin: 0 10rpx;
    }
  }
}

/* 猜你喜欢 */
.guess {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx;
  .guess-item {
    width: 345rpx;
    padding: 24rpx 20rpx 20rpx;
    margin-bottom: 20rpx;
    border-radius: 10rpx;
    overflow: hidden;
    background-color: #fff;
  }
  .image {
    width: 304rpx;
    height: 304rpx;
  }
  .name {
    height: 75rpx;
    margin: 10rpx 0;
    font-size: 26rpx;
    color: #262626;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .price {
    line-height: 1;
    padding-top: 4rpx;
    color: #cf4444;
    font-size: 26rpx;
  }
  .small {
    font-size: 80%;
  }
}
// 加载提示文字
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}
</style>
